package com.home.cop.system;

import com.home.cop.utils.CollectionUtils;

import java.util.Map;

public class Result {
    private Integer code = 200;
    private boolean             success;
    private String              status;
    private String              message;
    private Object              data;
    private Map<String, Object> pushData;

    public Result() {
    }

    /**
     * @param success 是否成功
     * @param message 消息
     */
    public Result(boolean success, String message) {
        this.success = success;
        this.message = message;
    }

    /**
     * @param success
     * @param status
     * @param message
     * @param data
     */
    public Result(boolean success, String status, String message, Object data) {
        this.success = success;
        this.status = status;
        this.message = message;
        this.data = data;
    }

    /*
     * 一些方便的返回方法 by xujiahong since 2017-04-18
     * status 字段似乎用不上
     */
    public static Result returnSuccess() {
        return new Result(true, "操作成功");
    }

    public static Result returnFail() {
        return new Result(false, "操作失败");
    }

    public static Result returnFail(String message) {
        return new Result(false, message);
    }

    public static Result returnSuccess(Object data) {
        return new Result(true, "操作成功").setData(data);
    }

    public static Result returnFail(String message, Object data) {
        return new Result(false, message).setData(data);
    }

    public static Result returnSuccess(Object data, String status) {
        return new Result(true, "操作成功").setData(data).setStatus(status);
    }

    public static Result returnFail(String message, Object data, String status) {
        return new Result(false, message).setData(data).setStatus(status);
    }

    public Result pushData(String key, Object value) {
        if (this.pushData == null) {
            this.pushData = CollectionUtils.newHashMap();
        }
        this.pushData.put(key, value);
        return this;
    }

    public static Result returnByResult(boolean rs) {
        String tipmassage = "操作失败";
        if (rs) {
            tipmassage = "操作成功";
        }
        return new Result().setSuccess(rs).setMessage(tipmassage);
    }

    public boolean isSuccess() {
        return success;
    }

    public Result setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public String getStatus() {
        return status;
    }

    public Result setStatus(String status) {
        this.status = status;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public Result setMessage(String message) {
        this.message = message;
        return this;
    }

    public Object getData() {
        if (null != this.pushData) {
            setData(pushData);
        }
        return data;
    }

    public Result setData(Object data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public Result setCode(Integer code) {
        this.code = code;
        return this;
    }

}
